package etf.db40;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.db4o.ObjectSet;
import com.db4o.query.Query;

import etf.library.LibraryLogo;

public class BlobDb4o extends Db4oUtil{
	
	final static String DB4OFILENAME = "BlobTest";
	
	public BlobDb4o(){
		createDatabase(DB4OFILENAME);
	}
	public BlobDb4o(String fileName){
		createDatabase("/" + fileName + ".db40");
	}

	public long insertBLOB(String name) {
		long elapsedTime = 0;
		try {
			long startTime = System.currentTimeMillis();
			
			BlobClass blob = new BlobClass(name + ".jpg");
			String location = "images/" + name + ".jpg";
			File file = new File(location);
			db.store(blob);
			blob.getImage().readFrom(file);
			
			db.commit();
			long stopTime = System.currentTimeMillis();
			elapsedTime = stopTime - startTime;
		} catch (Exception e) {
			e.getMessage();
			e.printStackTrace();
			db.close();
		}
		
		return elapsedTime;
	}
	
	public long insertBLOB2(String name) {
		long elapsedTime = 0;
		try {
			long startTime = System.currentTimeMillis();
			
			LibraryLogo ll = new LibraryLogo(name + ".jpg");
	
			String location = "images/" + name + ".jpg";
			
			BufferedImage originalImage =  ImageIO.read(new File(location));
	        //convert BufferedImage to byte array
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageIO.write( originalImage, "jpg", baos );
			baos.flush();
			byte[] imageInByte = baos.toByteArray();
			
			ll.setImage(imageInByte);
			
			db.store(ll);
			db.commit();
			long stopTime = System.currentTimeMillis();
			elapsedTime = stopTime - startTime;
		} catch (Exception e) {
			e.getMessage();
			e.printStackTrace();
			db.close();
		}
		
		return elapsedTime;
	}
	
	public long readBLOB(String name) {
		long elapsedTime = 0;
		try {
			long startTime = System.currentTimeMillis();
		
			Query q = db.query();
			q.constrain(BlobClass.class);
			q.descend("title").constrain(name + ".jpg");
			ObjectSet<BlobClass> result = q.execute();
			if (result.hasNext()) {
				BlobClass blob = result.next();
				blob.getImage().writeTo(new File("D:\\xdb4o\\" + blob.getTitle()));
			}
			
			long stopTime = System.currentTimeMillis();
			elapsedTime = stopTime - startTime;
		} catch (Exception e) {
			e.getMessage();
			e.printStackTrace();
			db.close();
		}
		
		return elapsedTime;
	}
	
	public long readBLOB2(String name) {
		long elapsedTime = 0;
		try {
			long startTime = System.currentTimeMillis();
		
			Query q = db.query();
			q.constrain(LibraryLogo.class);
			q.descend("title").constrain(name + ".jpg");
			ObjectSet<LibraryLogo> result = q.execute();
			if (result.hasNext()) {
				LibraryLogo ll = result.next();
				InputStream in = new ByteArrayInputStream(ll.getImage());
				BufferedImage bImageFromConvert = ImageIO.read(in);
			 
				ImageIO.write(bImageFromConvert,"jpg",new File("D:\\xdb4o2\\"+ll.getTitle()));
			}
			
			long stopTime = System.currentTimeMillis();
			elapsedTime = stopTime - startTime;
		} catch (Exception e) {
			e.getMessage();
			e.printStackTrace();
			db.close();
		}
		
		return elapsedTime;
	}
}
